/***********************************************************************************************************************************
 * Name:	History_Purge.sql
 * Author:	Frank Figearo (frank@sqlnerd.me)
 * Summary:	Purge history records older than 5 weeks.
 */
SET IMPLICIT_TRANSACTIONS OFF;
USE msdb;
DECLARE @oldest_date DATETIME= CAST(DATEADD(wk, -5, GETDATE()) AS DATE);
EXECUTE msdb.dbo.sp_purge_jobhistory Null, Null, @oldest_date;
EXECUTE msdb.dbo.sp_delete_backuphistory @oldest_date;
DELETE msdb.dbo.sysdbmaintplan_history WHERE end_time <= @oldest_date;
GO